package com.gitee.wsl.mathematics.geometry.d2

import com.gitee.wsl.mathematics.coordinate.d2.Coordinate2
import com.gitee.wsl.mathematics.geometry.api.PointContainer
import com.gitee.wsl.mathematics.number.compareTo
import com.gitee.wsl.mathematics.vector.ext.distanceTo

interface Point2Container<N:Number,V: Coordinate2<N, V>>:PointContainer<N,V> {

    override fun nearestVertex(pt: V): V?{
        var ret:V? = null
        var dist = NaN
        proxy.values().map {
            val dir = it.distanceTo(pt)
            if(dir < dist || dist == NaN){
                ret = it
                dist = dir
            }
        }
        return ret
    }

    override fun farthestVertex(pt: V): V? {
        var ret:V? = null
        var dist = NaN
        proxy.values().map {
            val dir = it.distanceTo(pt)
            if(dir > dist || dist == NaN){
                ret = it
                dist = dir
            }
        }
        return ret
    }

    override fun nearToFarVertexes(pt: V): Collection<V> {
        return proxy.values().sortedBy { it.distanceTo(pt).toDouble() }
    }

    override fun farToNearVertexes(pt: V): Collection<V>  = nearToFarVertexes(pt).reversed()
}